package com.gx.smart.building.ui.knowledge.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.gx.smart.base.BaseAdapter;
import com.gx.smart.building.R;
import com.gx.smart.building.databinding.ItemKnowledgeTypeBinding;
import com.gx.smart.building.ui.knowledge.KnowledgeArticleActivity;
import com.gx.smart.building.ui.knowledge.model.KnowledgeType;

/**
 * @author: xiaosy
 * 时间: 2022/4/11
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class ItemKnowledgeTypeAdapter extends BaseAdapter<KnowledgeType, ItemKnowledgeTypeAdapter.ViewHolder> {
    public ItemKnowledgeTypeAdapter(Context context) {
        super(context);
    }

    @Override
    protected int onBindLayout() {
        return R.layout.item_knowledge_type;
    }

    @Override
    protected ViewHolder onCreateHolder(View view) {
        ItemKnowledgeTypeBinding binding = ItemKnowledgeTypeBinding.inflate(LayoutInflater.from(mContext),
                (ViewGroup) view, false);
        return new ViewHolder(binding);
    }

    @Override
    protected void onBindData(ViewHolder holder, KnowledgeType item, int position) {
        holder.binding.type.setText(item.getName());
        ItemKnowledgeTypeItemAdapter adapter = new ItemKnowledgeTypeItemAdapter(mContext);
        holder.binding.recyclerView.setAdapter(adapter);
        adapter.setItemClickListener((OnItemClickListener<KnowledgeType>) (subItem, position1) -> {
            Intent intent = new Intent(mContext, KnowledgeArticleActivity.class);
            intent.putExtra("id", subItem.getId());
            mContext.startActivity(intent);
        });
        adapter.addAll(item.getChildren());
    }

    protected static class ViewHolder extends RecyclerView.ViewHolder {
        private final ItemKnowledgeTypeBinding binding;

        public ViewHolder(ItemKnowledgeTypeBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
